<?php
include APPLICATION_PATH."/models/base/OrderBase.php";
/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
class Model_Order extends Model_OrderBase{
    
}
class Model_OrderMapper extends Model_OrderMapperBase{
     public static function findbyid($order_no, Model_Order $entry) {
        $db = Zend_Db_Table::getDefaultAdapter();
        $select = 'select * from `order` where order_no=' . $order_no . '';
        $stmt = $db->query($select);
        $row = $stmt->fetchAll(PDO::FETCH_CLASS);
        $stmt->closeCursor();
        if ($row == NULL) {
            return NULL;
        } else {
            $entry->setId($row[0]->id)
                    ->setId($row[0]->id)
                    ->setStore_Id($row[0]->store_id)
                    ->setCreated($row[0]->created)
                    ->setCreated_By($row[0]->created_by)
                    ->setTotal_Charges($row[0]->total_charges)
                    ->setTotal_Price($row[0]->total_price)
                    ->setOrder_Status_Id($row[0]->order_status_id)
                    ->setOrder_No($row[0]->order_no);
        }
    }

    public static function fetchAllByKinhDoanh_Id($admin_id) {
         $db = Zend_Db_Table::getDefaultAdapter();
         $select = "Select `order`.id as order_id, agent.name as agent_name, `order`.order_no,`order`.total_charges,`order`.created,`order`.order_status_id  "
                 . "From `order` left join agent on `order`.store_id = agent.id "
                 . "Where agent.province_id in (Select admin_area.area_id from admin left join admin_area on admin.id= admin_area.admin_id where admin.id='".$admin_id."')
                    ORDER BY `order`.id desc";
        $stmt=$db->query($select);
         $stmt=$db->query($select);
        $rows=$stmt->fetchAll(PDO::FETCH_CLASS);
        $stmt->closeCursor();
        $entries   = array();
        foreach ($rows as $row){
            $array= array(
                'order_id'=>$row->order_id,
                'agent_name'=>$row->agent_name,
                'order_no'=>$row->order_no,
                'total_charges'=>$row->total_charges,
                'created'=>$row->created,
                'order_status'=>$row->order_status_id                
            );
            array_push($entries, $array);
        }
        return $entries; 
    }
    
    public static function fetchAllByAdmin() {
         $db = Zend_Db_Table::getDefaultAdapter();
         $select = "Select `order`.id as order_id, agent.name as agent_name, `order`.order_no,`order`.total_charges,`order`.created,`order`.order_status_id  "
                 . "From `order` left join agent on `order`.store_id = agent.id 
                     ORDER BY `order`.id desc";
                
        $stmt=$db->query($select);
         $stmt=$db->query($select);
        $rows=$stmt->fetchAll(PDO::FETCH_CLASS);
        $stmt->closeCursor();
        $entries   = array();
        foreach ($rows as $row){
            $array= array(
                'order_id'=>$row->order_id,
                'agent_name'=>$row->agent_name,
                'order_no'=>$row->order_no,
                'total_charges'=>$row->total_charges,
                'created'=>$row->created,
                'order_status'=>$row->order_status_id                
            );
            array_push($entries, $array);
        }
        return $entries; 
    }
    
}
